function querygame() {
    var beginTimes=""
    if($("#beginTime").val()!=""){
        beginTimes=$("#beginTime").val()+' '+'00:00:00'
    }else {
        beginTimes=""
    }
    var endTimes=""
    if($("#endTime").val()!=""){
        endTimes=$("#endTime").val()+' '+'23:59:59'
    }else {
        endTimes=""
    }
    var createTimeBegins=""
    if($("#createTimeBegin").val()!=""){
        createTimeBegins=$("#createTimeBegin").val()+' '+'00:00:00'
    }else {
        createTimeBegins=""
    }
    var createTimeEnds=""
    if($("#createTimeEnd").val()!=""){
        createTimeEnds=$("#createTimeEnd").val()+' '+'23:59:59'
    }else {
        createTimeEnds=""
    }
    $.ajax({
        url: 'https://'+ window.location.host +'/api/admin/apromotion/lotteryDraw/listLotteyDraw',
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum":"1",
            "pageSize":"30",
            "lotteryAfterName":$("#lotteryAfterName").val(),
            "lotteryBeforeName":$("#lotteryBeforeName").val(),
            "beginTime":beginTimes,
            "endTime":endTimes,
            "createTimeBegin":createTimeBegins,
            "createTimeEnd":createTimeEnds,
            "source":$("#source option:selected").val(),
            "joinRequire":$("#joinRequire option:selected").val(),
            "limitWay":$("#limitWay option:selected").val(),
            "status":$("#status option:selected").val(),
            "activityId":$("#activityId").val()
        }),
        success: function (data) {
            var pk=""
            if(data.data.result!=null){
                pk=data.data.totalPage
                couponlist1(data.data.result);
            }else {
                couponlist1("");
                pk=0
            }
            $('.M-box3').pagination({
                pageCount:pk,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    console.log(api)
                    $.ajax({
                        url: 'https://'+ window.location.host +'/api/admin/apromotion/lotteryDraw/listLotteyDraw',
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageNum":api.getCurrent(),
                            "pageSize":30,
                            "lotteryAfterName":$("#lotteryAfterName").val(),
                            "lotteryBeforeName":$("#lotteryBeforeName").val(),
                            "beginTime":beginTimes,
                            "endTime":endTimes,
                            "createTimeBegin":createTimeBegins,
                            "createTimeEnd":createTimeEnds,
                            "source":$("#source option:selected").val(),
                            "joinRequire":$("#joinRequire option:selected").val(),
                            "limitWay":$("#limitWay option:selected").val(),
                            "status":$("#status option:selected").val(),
                            "activityId":$("#activityId").val()
                        }),
                        success: function (result) {
                            couponlist1(result.data.result);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function couponlist1(data, index){
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组
    $.each(data, function (i, field) {
        var rowData = new Array();
        rowData.push(field.activityId);
        rowData.push(field.lotteryAfterName);
        rowData.push(field.lotteryBeforeName);
        rowData.push(field.startTime);
        rowData.push(field.endTime);
        rowData.push(field.source);
        rowData.push(field.joinRequire);
        rowData.push(field.limitWay);
        rowData.push(field.status);
        rowData.push(field.createTime);
        rowData.push(field.createName);
        rowData.push(field.lotteryId);
        dataSource.push(rowData);

    });

    initData(dataSource);

    function initData(dataSource) {

        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "order": [[ 10, "desc" ]],
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": false,
            "destroy": false,
            "bAutoWidth": false,
            "bLengthChange": false,
            "aLengthMenu":[10,20,30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },

                {
                    "render": function (data, type, row, meta) {

                        var shijian1 = '';
                        var date = row[3];
                        var split = date.split(" ");
                        shijian1 = split[0] + '<br/>' + split[1];
                        var shijian2 = '';
                        var date2 = row[4];
                        var split2 = date2.split(" ");
                        shijian2 = split2[0] + '<br/>' + split2[1];
                        var val = shijian1+'<br>~<br>'+shijian2

                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val =""
                        if (row[5].match('10') &&row[5].match('20')&&row[5].match('30') ) {
                            val = '不限'
                        }else if(row[5].match('10')){
                            val = 'PC'
                        }else if(row[5].match('20')){
                            val = 'APP'
                        }else if(row[5].match('30')){
                            val = 'H5'
                        }else {

                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if (row[6] == 10) {
                            val = '免费参加'
                        }else  if (row[6] == 20){
                            val = '新用户注册'
                        }else  if (row[6] == 30){
                            val = '积分消耗'
                        }else  if (row[6] == 40){
                            val = '累计订单数量'
                        }else  if (row[6] == 50){
                            val = '累计满金额'
                        }else  if (row[6] == 60){
                            val = '单笔订单满金额'
                        }
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if (row[7] == 10) {
                            val = '固定次数'
                        }else   if (row[7] == 20){
                            val = '周期次数'
                        }
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = ""
                        if (row[8] ==10) {
                            val = '未开始'
                        }else if (row[8] ==20){
                            val = '进行中'
                        }else if (row[8] ==30){
                            val = '已结束'
                        }else if (row[8] ==40){
                            val = '已取消'
                        }else if (row[8] ==50){
                            val = '已失效'
                        }
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = '';
                        var date = row[9];
                        var split = date.split(" ");
                        val = split[0] + '<br/>' + split[1];
                        return val;
                    },
                    "targets":8
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[10]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 9
                },
                {
                    "render": function (data, type, row, meta) {
                        var val=""
                        if (row[8] ==10) {
                            val = '<a onclick="viewdetails(\'查看\',\'in_progress_view.html\',\''+row[11]+'\',\''+row[0]+'\',1)">查看</a>&nbsp;&nbsp;<a onclick="viewdetails(\'编辑\',\'create_rules.html\',\''+row[11]+'\',\''+row[0]+'\',0)">修改</a>&nbsp;&nbsp;<a onclick="quxiao(\''+row[11]+'\',\''+row[0]+'\')">取消</a>'
                        }else if (row[8] ==20){
                            val = '<a onclick="viewdetails(\'查看\',\'in_progress_view.html\',\''+row[11]+'\',\''+row[0]+'\',1)">查看</a>&nbsp;&nbsp;<a onclick="viewdetails(\'中奖名单\',\'winning.html\',\''+row[11]+'\',\''+row[0]+'\',1)">中奖名单</a>&nbsp;&nbsp;<a onclick="quxiao(\''+row[11]+'\',\''+row[0]+'\')">强制取消</a>'
                        }else if (row[8] ==30){
                            val = '<a onclick="viewdetails(\'查看\',\'in_progress_view.html\',\''+row[11]+'\',\''+row[0]+'\',1)">查看</a>&nbsp;&nbsp;<a onclick="viewdetails(\'中奖名单\',\'winning.html\',\''+row[11]+'\',\''+row[0]+'\',1)">中奖名单</a>'
                        }else if (row[8] ==40){
                            val = '<a onclick="viewdetails(\'查看\',\'in_progress_view.html\',\''+row[11]+'\',\''+row[0]+'\',1)">查看</a>'
                        }else if (row[8] ==50){
                            val = '<a onclick="viewdetails(\'查看\',\'in_progress_view.html\',\''+row[11]+'\',\''+row[0]+'\',1)">查看</a>&nbsp;&nbsp;<a onclick="viewdetails(\'中奖名单\',\'winning.html\',\''+row[11]+'\',\''+row[0]+'\',1)">中奖名单</a>'
                        }
                        return val;
                    },
                    "targets": 10
                },

            ]
        });

    };
}
function viewdetails(title,url,data,activityids,idss){
    if(idss==0){
        sessionStorage.setItem("gamestare","1")
    }
    sessionStorage.setItem("viewdetail",data);
    sessionStorage.setItem("activityids",activityids)
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
//取消
function quxiao(val) {
    var curInd = layer.confirm('<span id="status" style="width:200px;height:100px;resize: none;" maxlength="200" >确定要取消此次活动吗？</span>', {title:'提示',btn: ['确定', '取消']}, function(index){

        $.ajax({
            url: 'https://'+ window.location.host +'/api/admin/apromotion/lotteryDraw/cancelLotteryDraw',
            cache: false, //禁用缓存
            dataType: "json",
            type:"post",
            data:{
                "lotteryId":val,
            },
            success: function (data) {
              if(data.errorCode==0){
                  querygame()
              }else {
                  $.Huimodalalert(data.message,2000);
              }
            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        });
        layer.close(curInd);
    },function(){
        layer.close(curInd);

    });
}